package com.cloudfunx.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author linjiayu
 */
public class ListBeanUtil {

    /**
     * 将原始List的内容转为另一种类型的List
     * @param sourceList  原始集合
     * @param targetClazz 需要转成的类型
     * @return 普通集合
     */
    public static <T> List<T> convert(List sourceList, Class<T> targetClazz) {
        List<T> targetList = new ArrayList<>();
        for (Object o : sourceList) {
            T m = BeanUtils.instantiateClass(targetClazz);
            BeanUtils.copyProperties(o, m);
            targetList.add(m);
        }
        return targetList;
    }

    public static <T> Grid<T> convert(Grid<?> grid, Class<T> targetClazz) {
        Grid<T> result = new Grid<>();
        if (Objects.nonNull(grid)) {
            result.setTotal(grid.getTotal());
            result.setPages(grid.getPages());
            List<?> list = grid.getList();
            if (!CollectionUtils.isEmpty(list)) {
                result.setList(convert(list, targetClazz));
            }
        }
        return result;
    }

    public static <T> Grid<T> convert(Page<?> buildPage, List<T> list) {
        Grid<T> g = new Grid<>();
        g.setPages(new Long(buildPage.getPages()).intValue());
        g.setTotal(new Long(buildPage.getTotal()).intValue());
        if (!CollectionUtils.isEmpty(list)) {
            g.setList(list);
        }
        return g;
    }

    public static <T> Grid<T> convert(Page<?> buildPage, List<?> list, Class<T> cla) {
        Grid<T> g = new Grid<>();
        g.setPages(new Long(buildPage.getPages()).intValue());
        g.setTotal(new Long(buildPage.getTotal()).intValue());
        if (CollectionUtils.isEmpty(list)) {
            return g;
        }
        g.setList(convert(list, cla));
        return g;
    }

    public static <T> Grid<T> convert(IPage<?> buildPage, Class<T> cla) {
        Grid<T> g = new Grid<>();
        g.setPages(new Long(buildPage.getPages()).intValue());
        g.setTotal(new Long(buildPage.getTotal()).intValue());
        g.setList(convert(buildPage.getRecords(), cla));
        return g;
    }

    @SneakyThrows
    public static <T> List<T> toSingleList(List<?> list, Class<T> clz, String methodName) {
        if (list != null && list.size() > 0) {
            List<T> r = new ArrayList<>(list.size());
            for (Object o : list) {
                Class<?> aClass = o.getClass();
                Method method = aClass.getMethod(methodName);
                Object invoke = method.invoke(o);
                r.add((T) invoke);
            }
            return r;
        }
        return new ArrayList<>();
    }

    public static void copyProperties(Object source, Object target) {
        copyProperties(source, target, (String[]) null);
    }

    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        if (source == null) {
            return;
        }
        BeanUtils.copyProperties(source, target, ignoreProperties);
    }

}
